<html><!-- Created using the cpp_pretty_printer from the dlib C++ library.  See http://dlib.net for updates. --><head><title>dlib C++ Library - server_iostream_ex.cpp</title></head><body bgcolor='white'><pre>
<font color='#009900'>// The contents of this file are in the public domain. See LICENSE_FOR_EXAMPLE_PROGRAMS.txt
</font><font color='#009900'>/*

    This is an example illustrating the use of the server_iostream object from
    the dlib C++ Library.

    This is a simple echo server.  It listens on port 1234 for incoming
    connections and just echos back any text it receives, but in upper case.  So
    basically it is the same as the <a href="sockets_ex.cpp.html">sockets_ex.cpp</a> example program  except it
    uses iostreams. 

    To test it out you can just open a command prompt and type:
    telnet localhost 1234

    Then you can type away.

*/</font>




<font color='#0000FF'>#include</font> <font color='#5555FF'>&lt;</font>dlib<font color='#5555FF'>/</font>server.h<font color='#5555FF'>&gt;</font>
<font color='#0000FF'>#include</font> <font color='#5555FF'>&lt;</font>iostream<font color='#5555FF'>&gt;</font>

<font color='#0000FF'>using</font> <font color='#0000FF'>namespace</font> dlib;
<font color='#0000FF'>using</font> <font color='#0000FF'>namespace</font> std;



<font color='#0000FF'>class</font> <b><a name='serv'></a>serv</b> : <font color='#0000FF'>public</font> server_iostream
<b>{</b>

    <font color='#0000FF'><u>void</u></font> <b><a name='on_connect'></a>on_connect</b>  <font face='Lucida Console'>(</font>
        std::istream<font color='#5555FF'>&amp;</font> in,
        std::ostream<font color='#5555FF'>&amp;</font> out,
        <font color='#0000FF'>const</font> std::string<font color='#5555FF'>&amp;</font> foreign_ip,
        <font color='#0000FF'>const</font> std::string<font color='#5555FF'>&amp;</font> <font color='#009900'>/*local_ip*/</font>,
        <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>short</u></font> <font color='#009900'>/*foreign_port*/</font>,
        <font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>short</u></font> <font color='#009900'>/*local_port*/</font>,
        uint64 <font color='#009900'>/*connection_id*/</font>
    <font face='Lucida Console'>)</font>
    <b>{</b>
        <font color='#009900'>// The details of the connection are contained in the last few arguments to
</font>        <font color='#009900'>// on_connect().  For more information, see the documentation for the
</font>        <font color='#009900'>// server_iostream.  However, the main arguments of interest are the two streams.
</font>        <font color='#009900'>// Here we also print the IP address of the remote machine.
</font>        cout <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>Got a connection from </font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> foreign_ip <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> endl;

        <font color='#009900'>// Loop until we hit the end of the stream.  This happens when the connection
</font>        <font color='#009900'>// terminates.
</font>        <font color='#0000FF'>while</font> <font face='Lucida Console'>(</font>in.<font color='#BB00BB'>peek</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>!</font><font color='#5555FF'>=</font> EOF<font face='Lucida Console'>)</font>
        <b>{</b>
            <font color='#009900'>// get the next character from the client
</font>            <font color='#0000FF'><u>char</u></font> ch <font color='#5555FF'>=</font> in.<font color='#BB00BB'>get</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;

            <font color='#009900'>// now echo it back to them
</font>            out <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>char</u></font><font face='Lucida Console'>)</font><font color='#BB00BB'>toupper</font><font face='Lucida Console'>(</font>ch<font face='Lucida Console'>)</font>;
        <b>}</b>
    <b>}</b>

<b>}</b>;


<font color='#0000FF'><u>int</u></font> <b><a name='main'></a>main</b><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>
<b>{</b>
    <font color='#0000FF'>try</font>
    <b>{</b>
        serv our_server;

        <font color='#009900'>// set up the server object we have made
</font>        our_server.<font color='#BB00BB'>set_listening_port</font><font face='Lucida Console'>(</font><font color='#979000'>1234</font><font face='Lucida Console'>)</font>;
        <font color='#009900'>// Tell the server to begin accepting connections.
</font>        our_server.<font color='#BB00BB'>start_async</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;

        cout <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> "<font color='#CC0000'>Press enter to end this program</font>" <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> endl;
        cin.<font color='#BB00BB'>get</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
    <b>}</b>
    <font color='#0000FF'>catch</font> <font face='Lucida Console'>(</font>exception<font color='#5555FF'>&amp;</font> e<font face='Lucida Console'>)</font>
    <b>{</b>
        cout <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> e.<font color='#BB00BB'>what</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>&lt;</font><font color='#5555FF'>&lt;</font> endl;
    <b>}</b>
<b>}</b>



</pre></body></html>